import router from '@/router/index'
import useStore from '@/stores/index'
import { ElMessage } from 'element-plus'
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const list = ['/login']
function time(): boolean | undefined {
  const newTime: number = Date.now()
  console.log(newTime)
  // console.log(useStore().user.time, '1')

  const loginTime = localStorage.getItem('loginTime')
  if (newTime - Number(loginTime) >= Number(localStorage.getItem('time'))) {
    return true
  }
}
router.beforeEach((to, from): boolean | object => {
  NProgress.start()
  // return true
  if (useStore().user.token) {
    if (to.path === '/login') {
      return false
    } else {
      if (time()) {
        useStore().user.out()
        ElMessage.error('登录超时，请重新登录')
      }
      return true
    }
  } else {
    if (list.includes(to.path)) {
      return true
    } else {
      return { path: '/login' }
    }
  }
})
router.afterEach((): void => {
  NProgress.done()
})
